//
//  LWDiscountTabVc.m
//  LWShopItem
//
//  Created by mac on 17/5/19.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWDiscountTabVc.h"
#import "UIColor+Addition.h"
#import "LWHaveusedViewController.h"
#import "LWUNusedViewController.h"
#import "LWUsedViewController.h"

#import "UIViewController+CZAddition.h"
#import "LWCatrgoryView.h"
#import <Masonry.h>



@interface LWDiscountTabVc ()<UIScrollViewDelegate>

/**
 *  顶部视图
 */
@property (weak, nonatomic) UIView *headerView;

/**
 *  中间的分类视图
 */
@property (weak, nonatomic) LWCatrgoryView *categoryView;

/**
 *  滚动视图
 */
@property (weak, nonatomic) UIScrollView *scrollView;


@end

@implementation LWDiscountTabVc

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupUI];
    
}

#pragma mark - 监听事件
// 分类视图的值改变事件
- (void)categoryViewValuechanged:(LWCatrgoryView *)sender {
    
    NSLog(@"%zd", sender.pageNumber);
    // 让scrollView滚动
    CGRect rect = CGRectMake(_scrollView.bounds.size.width * sender.pageNumber, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    // 让scrollView滚动,把rect区域的内容展示出来!
    [_scrollView scrollRectToVisible:rect animated:YES];
    
}

#pragma mark - 代理方法
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 0.如果不是用户滚动的,就不要设置偏移量了!
    if (!(scrollView.isDecelerating || scrollView.isDragging || scrollView.isTracking)) {
        return;
    }
    
    // 1.获取内容的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    //    NSLog(@"%f", offsetX);
    // 2.设置偏移距离给分类视图
    // 分类视图黄条的移动距离为 offsetX 的 3 分之 1
    _categoryView.offset_x = offsetX / 3;
    
}

// 测试代码! 演示scrollView的滚动
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    CGRect rect = CGRectMake(_scrollView.bounds.size.width * 0, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    // 让scrollView滚动,把rect区域的内容展示出来!
    [_scrollView scrollRectToVisible:rect animated:YES];
    
}

#pragma MARK - UI
- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"优惠券";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:0x9ACD32];
    
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(popClick)];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    
    UIView *header = [[UIView alloc] init];
    header.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:header];
    
    [header mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo(self.view);
        
        make.height.mas_equalTo(64);
        
    }];

    
    //顶部视图
    LWCatrgoryView *headerV = [[LWCatrgoryView alloc]init];
    
    headerV.backgroundColor = [UIColor whiteColor];
    
    
    
    // 2.2 监听事件 值改变事件
    [headerV addTarget:self action:@selector(categoryViewValuechanged:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:headerV];
    
    [headerV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(header.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(37);
    }];
    
    
    // 3.底部的滚动视图 UIScrollView!
    UIScrollView *scrollV = [self setupContentView];
    
    [self.view addSubview:scrollV];
    
    [scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(headerV.mas_bottom);
        make.left.right.equalTo(headerV);
        make.bottom.equalTo(self.view);
        
    }];
    
    // MARK: - 4.添加拖拽手势识别器
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
//    [self.view addGestureRecognizer:pan];
    
    // MARK: - 5.记录成员变量
    _headerView = headerV;
    _categoryView = headerV;
    _scrollView = scrollV;




    

}

#pragma mark - 负责添加底部控制器视图的方法,并返回scrollView
- (UIScrollView *)setupContentView {
    
    UIScrollView *scrollV = [[UIScrollView alloc] init];
    scrollV.backgroundColor = [UIColor whiteColor];
    scrollV.showsHorizontalScrollIndicator = NO;
    scrollV.pagingEnabled = YES;
    
    // 设置代理!
    scrollV.delegate = self;
    
    // MARK: - 添加控制器的视图
    NSArray<NSString *> *clsNameArr = @[@"LWHaveusedViewController", @"LWUNusedViewController", @"LWUsedViewController"];
    
    

    
    // 定义可变数组,负责存放底部控制器视图的view!
    NSMutableArray<UIView *> *tempArrM = [NSMutableArray array];
    
    [clsNameArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        // 根据字符串动态的创建类 & 对象
        Class cls = NSClassFromString(obj);
        
        UIViewController *vc = [[cls alloc] init];
        
        NSAssert([vc isKindOfClass:[UIViewController class]], @"%@ 控制器类名 写的不对!", obj);
        
        // 添加控制器视图!
        [self cz_addChildController:vc intoView:scrollV];
        
        // 添加view
        [tempArrM addObject:vc.view];
    }];
    
    // 布局控制器的视图
    // 三个摆开,x!
    [tempArrM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    // y size!
    [tempArrM mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.equalTo(scrollV);
        
        // 顶部距离 + 底部距离 + 自己的高度 才能够决定contentSize的高度!
        make.top.bottom.equalTo(scrollV);
        
    }];
    
    return scrollV;
}


#pragma MARK - POP
- (void)popClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
